/*
 *  $Id: FilterRegions.cs 1011 2010-05-09 21:35:13Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using AgateLib.DisplayLib;
using AgateLib.Geometry;
using AgateLib.InputLib;

namespace Ragima.DeckManager {
    public abstract class FilterRegion: Region<DeckManagerScene> {
        private const string HintPrefix = "deckmanager.filter.hint.";

        private const int FilterY = 746;
        private const int FilterElementX = 700;
        private const int FilterTypeX = 913;
        private const int FilterSize = 12;
        private const int FilterStep = 14;

        private UI.Sprite _sprite;
        protected int _number;
        private Hint _hint;

        public FilterRegion(DeckManagerScene scene, bool isElement, int num): base(GetFilterRect(isElement, num), scene) {
            _sprite = null;
            _number = num;
            _hint = null;
        }

        private static Rectangle GetFilterRect(bool isElement, int num) {
            int x = isElement ? FilterElementX : FilterTypeX;
            return new Rectangle(x + num*FilterStep, FilterY, FilterSize, FilterSize);
        }

        protected UI.Sprite Sprite {
            get {
                if (_sprite == null) _sprite = Data.DeckManager.Buttons.GetSprite(SpriteId);
                return _sprite;
            }
        }

        protected abstract string SpriteId { get; }

        protected abstract bool Active { get; }

        protected abstract string HintPhraseId { get; }

        public override Hint GetHint() {
            if (_hint == null) {
                Languages.Phrase phrase = new Languages.Phrase(HintPrefix+HintPhraseId);
                _hint = new TextHint(Rect, phrase.GetText());
            }
            return _hint;
        }

        public override void Draw() {
            int num = _number * 2;
            if (Active) num++;
            Sprite.Draw(num, Rect.Location);
        }

        protected static bool ShiftPressed {
            get { return Keyboard.Keys[KeyCode.Shift]; }
        }
    }

    public class ElementFilterRegion: FilterRegion {
        public readonly ElementType Element;

        public ElementFilterRegion(DeckManagerScene scene, ElementType element): base(scene, true, element.Number) {
            Element = element;
        }

        protected override string SpriteId {
            get { return "elements"; }
        }

        protected override bool Active {
            get { return Scene.GetElementFilter(Element); }
        }

        protected override string HintPhraseId {
            get { return "element."+Element.Id; }
        }

        public override void OnClick() {
            Scene.SetElementFilter(Element, ShiftPressed);
        }
    }

    public class TypeFilterRegion: FilterRegion {
        private static readonly string[] CardTypeIds = { "creature", "land", "spell", "enchantment", "ability" };

        public readonly CardType CardType;

        public TypeFilterRegion(DeckManagerScene scene, CardType cardType): base(scene, false, DeckManagerScene.CardTypeReverse[(int) cardType]) {
            cardType = CardType;
        }

        protected override string SpriteId {
            get { return "types"; }
        }

        protected override bool Active {
            get { return Scene.GetTypeFilter(_number); }
        }

        protected override string HintPhraseId {
            get { return "type."+CardTypeIds[_number]; }
        }

        public override void OnClick() {
            Scene.SetTypeFilter(_number, ShiftPressed);
        }
    }

    public class AllFilterRegion: FilterRegion {
        private bool _isElement;

        public AllFilterRegion(DeckManagerScene scene, bool isElement): base(scene, isElement, isElement ? 7 : 5) {
            _isElement = isElement;
        }

        protected override string SpriteId {
            get { return "allfilter"; }
        }

        protected override bool Active {
            get { return _isElement ? Scene.GetElementFilterAll() : Scene.GetTypeFilterAll(); }
        }

        protected override string HintPhraseId {
            get { return _isElement ? "element.all" : "type.all"; }
        }

        public override void Draw() {
            Sprite.Draw(Active ? 0 : 1, Rect.Location);
        }

        public override void OnClick() {
            if (_isElement)
                Scene.SetElementFilterAll();
            else
                Scene.SetTypeFilterAll();
        }
    }
}
